package com.example.service.wechat.impl;

import com.example.pojo.wechat.AccessToken;
import com.example.pojo.wechat.OpenId_SessionKey;
import com.example.service.wechat.WxAuthenticService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;


/**
 * @ClassName WxAuthenticServiceImpl
 * @Description: com.example.service.wechat.impl
 * @Auther: xiwd
 * @Date: 2022/4/18 - 04 - 18 - 14:42
 * @version: 1.0
 */
@Service
@PropertySource(encoding = "UTF-8",value = "classpath:wxapplication.properties")
public class WxAuthenticServiceImpl implements WxAuthenticService {
    //properties
    @Value("${grant_type}")
    private String grant_type;
    @Value("${appID}")
    public String appID;
    @Value("${secret}")
    public String secret;

    //methods
    @Override
    public String getAccessToken() {
        //发送请求
        HttpClient httpClient = null;
        URIBuilder builder = null;
        HttpGet httpGet = null;
        HttpResponse httpResponse = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            httpClient = HttpClients.createDefault();
            builder = new URIBuilder("https://api.weixin.qq.com/cgi-bin/token");
            builder.addParameter("grant_type",grant_type);
            builder.addParameter("appid",appID);
            builder.addParameter("secret",secret);
            httpGet = new HttpGet(builder.build());
            httpResponse = httpClient.execute(httpGet);
            String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
            AccessToken accessToken = objectMapper.readValue(result, AccessToken.class);
            System.out.println(accessToken);
            return accessToken.getAccess_token();
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    @Override
    public OpenId_SessionKey getOpenId_SessionKey(String code) {
        HttpClient httpClient = null;
        URIBuilder uriBuilder = null;
        HttpGet httpGet = null;
        HttpResponse httpResponse = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            httpClient = HttpClients.createDefault();
            uriBuilder = new URIBuilder("https://api.weixin.qq.com/sns/jscode2session");
            //添加参数
            uriBuilder.addParameter("grant_type",grant_type);
            uriBuilder.addParameter("appid",appID);
            uriBuilder.addParameter("secret",secret);
            uriBuilder.addParameter("js_code",code);
            //创建GET请求对象
            httpGet = new HttpGet(uriBuilder.build());
            //创建响应对象
            httpResponse = httpClient.execute(httpGet);
            //获取响应结果
            String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
            System.out.println(result);
            return objectMapper.readValue(result,OpenId_SessionKey.class);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
